package Wallpaper::Execute::Collage;
$VERSION = sprintf "%.3f", 1 + substr(q$Revision: 162 $,9)/1000;

package Wallpaper::Execute;

use Image::Magick;

use Wallpaper::Configuration qw( %datastore );
use Wallpaper::Tools;

sub docollage {
    my ( $display, $image ) = @_;
    my $general_config = Wallpaper::Configuration->general;
    my $display_config = Wallpaper::Configuration->display( $display );
    my $collage_config = Wallpaper::Configuration->collage( $display );
    my $colorscheme    = $datastore{ $display }{ 'scheme' };

    die "Cannot read the upcoming image : " . $datastore{ $display }{ 'next' } if ( !-e $datastore{ $display }{ 'next' } );
    $image->Read( $datastore{ $display }{ 'next' } );               # Read in image for next
    die "Cannot read blank image : " . $general_config->{ 'bgdir' } . "/blank.png" if ( !-e $general_config->{ 'bgdir' } . '/blank.png' );
    $image->Read( $general_config->{ 'bgdir' } . '/blank.png' );    # Read in placeholder for nextimage collage

    $background = $image->[0];                                      # Reference to the tiled/mangled background
    $nextphoto  = $image->[1];                                      # Reference to the nextphoto (for collage)
    $nextfiller = $image->[2];                                      # Reference to the blank next filler collage

    # This is pretty hairy.
    $nextfiller->Resize( geometry => $collage_config->{ 'container' } );    # Create a large container for the collage
    $nextphoto->Resize( geometry  => $collage_config->{ 'photo' } );        # Resize the next photo brutally, forget chopping
    $nextphoto->Border( geometry => $collage_config->{ 'border1' }{ 'geo' }, fill => $collage_config->{ 'border1' }{ 'color' } );
    $nextphoto->Border( geometry => $collage_config->{ 'border2' }{ 'geo' }, fill => $collage_config->{ 'border2' }{ 'color' } );

    $nextphoto2 = $nextphoto->Clone();                                      # Clone it! YAH!
    $nextphoto3 = $nextphoto->Clone();
    $nextphoto4 = $nextphoto->Clone();
    $nextphoto5 = $nextphoto->Clone();

    # Now we rotate our clones and composite them in the collage container
    $nextphoto2->Rotate( degrees => &rotate, color => 'none' );
    $nextfiller->Composite( image => $nextphoto2, compose => 'SrcOver' );

    $nextphoto3->Rotate( degrees => &rotate, color => 'none' );
    $nextfiller->Composite( image => $nextphoto3, compose => 'SrcOver' );

    $nextphoto4->Rotate( degrees => &rotate, color => 'none' );
    $nextfiller->Composite( image => $nextphoto4, compose => 'SrcOver' );

    $nextphoto5->Rotate( degrees => &rotate, color => 'none' );
    $nextfiller->Composite( image => $nextphoto5, compose => 'SrcOver' );

    # Now make a shadow mask of the whole mess created above
    $nextshadow = $nextfiller->Clone();
    $nextshadow->Set( background => 'black' );
    $nextshadow->Shadow( opacity => '50', sigma => '2' );

    # And place the mask and collage down on the background.
    $background->Composite( image   => $nextshadow,
                            compose => 'atop',
                            gravity => 'SouthEast',
                            x       => '0',
                            y       => '-8'
                          );

    $background->Composite( image   => $nextfiller,
                            compose => 'atop',
                            gravity => 'SouthEast',
                            x       => '8',
                            y       => '0'
                          );

    # Now lets place some descriptive text down... (this is the shadow mask)
    $background->Annotate( font        => $collage_config->{ 'text' }{ 'font' },
                           pointsize   => $collage_config->{ 'text' }{ 'size' },
                           fill        => "#" . $colorscheme->{ 'primary' }{ 'lpale' },
                           stroke      => "#" . $colorscheme->{ 'primary' }{ 'dark' },
                           strokewidth => 1,
                           gravity     => 'SouthEast',
                           x           => $collage_config->{ 'text' }{ 'x' },
                           y           => $collage_config->{ 'text' }{ 'y' },
                           text        => $collage_config->{ 'text' }{ 'content' }
                         );

    # And plaster the mask with the real text.
    $background->Annotate( font      => $collage_config->{ 'text' }{ 'font' },
                           pointsize => $collage_config->{ 'text' }{ 'size' },
                           fill      => "#" . $colorscheme->{ 'primary' }{ 'lpale' },
                           stroke    => 'none',
                           gravity   => 'SouthEast',
                           x         => $collage_config->{ 'text' }{ 'x' },
                           y         => $collage_config->{ 'text' }{ 'y' },
                           text      => $collage_config->{ 'text' }{ 'content' }
                         );

    pop( @{ $image } );
    pop( @{ $image } );

  return $background;

} ## end sub docollage

=item * rotate

rotate simply returns a random number between 15 and 7.  It's used to provide a
rotation amount for the next photo collage.

Accepts no parameters, returns a number between 15 and 7.

=cut

sub rotate {
    my $degrees = rand() * 15 - 7;
  return $degrees;
}

1;
